<?php
/**
 * Author: yixuan
 * Desc:判断表不存在，则创建表
 * Time: 2016/5/27 14:25
 */

namespace app\model;

use think\Db;

class InstallNew
{

    public static function index()
    {
        $sql = "
                 CREATE TABLE IF NOT EXISTS `yx_sys_cache` (
                    `key` varchar(100) NOT NULL COMMENT '',
                    `value` longtext NOT NULL COMMENT '',
                    `timestamp` int(11) NOT NULL DEFAULT 0 COMMENT '过期时间戳',
                    PRIMARY KEY (`key`)
                 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='缓存表';

               CREATE TABLE IF NOT EXISTS  `yx_sys_session` (
                  `session_id` varchar(255) CHARACTER SET utf8 NOT NULL,
                  `session_expire` int(11) NOT NULL DEFAULT '0' COMMENT 'SESSION的过期时间',
                  `session_data` varchar(5000) not null COMMENT 'SESSION的数据内容',
                  `ip` varchar(255) CHARACTER SET utf8 NOT NULL DEFAULT '',
                  PRIMARY KEY (`session_id`),
                  UNIQUE KEY `session_id` (`session_id`),
                  KEY `NewIndex1` (`session_expire`)
                ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='缓存表';
                ";
        $result = db_run($sql);
    }

    /**
     * Desc:创建yx_sys_members表
     * User: yixuan
     * Time: 2016/6/17 15:18chuang
     */
    public static function create_members()
    {
        $sql = " CREATE TABLE IF NOT EXISTS `yx_sys_members` (
                `uid` INT(11) NOT NULL AUTO_INCREMENT,
                `mpid` INT(11) NOT NULL COMMENT '',
                `openid` VARCHAR(50) NOT NULL COMMENT '',
                `unionid` VARCHAR(50) NOT NULL COMMENT '',
                `nickname` VARCHAR(50) DEFAULT NULL COMMENT '昵称',
                `avatar` VARCHAR(255) DEFAULT NULL COMMENT '头像',
                `realname` VARCHAR(50) DEFAULT NULL COMMENT '姓名',
                `mobile` VARCHAR(20) DEFAULT NULL COMMENT '手机号',
                `updatetime` INT(11) NOT NULL  COMMENT '修改时间',
                PRIMARY KEY (`uid`),
                UNIQUE KEY `idx_openid` (`openid`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='粉丝总表';
        ";
        Db::execute($sql);
        if (empty(DB::query("SHOW INDEX FROM yx_sys_members WHERE Column_name = 'openid';"))) {
            Db::execute("ALTER TABLE `yx_sys_members` ADD INDEX `idx_openid` (`openid`)");
        }
    }

    public static function create_paylog()
    {
        $sql = "  CREATE TABLE IF NOT EXISTS `yx_sys_paylog` (
                    `id` bigint(11) unsigned NOT NULL
                    AUTO_INCREMENT,
                    `openid` varchar(40) NOT NULL,
                    `mpid` int(11) NOT NULL,
                    `uniontid` varchar(64) NOT NULL,
                    `tid` varchar(128) NOT NULL,
                    `fee` decimal(10,2) NOT NULL,
                    `status` tinyint(4) NOT NULL,
                    `module` varchar(50) NOT NULL,
                    `tag` varchar(2000) NOT NULL,
                    PRIMARY KEY (`id`),
                    KEY `uniontid` (`uniontid`),
                    KEY `idx_tid` (`tid`),
                    KEY `idx_openid` (`openid`),
                    KEY `idx_mpid` (`mpid`)
                ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='支付日志表';
";
        Db::execute($sql);
    }

    /**
     * Desc:创建附件表
     * User: yixuan
     * Time: 2016/6/17 15:17
     */
    public static function create_attachment()
    {
        $sql = "  CREATE TABLE IF NOT EXISTS `yx_sys_attachment` (
                  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
                  `mpid` int(10) unsigned NOT NULL COMMENT '所属账号mpid',
                  `uid` int(10) unsigned DEFAULT NULL COMMENT '上传人的uid',
                  `filename` varchar(255) NOT NULL COMMENT '文件名',
                  `attachment` varchar(255) NOT NULL COMMENT '文件路径',
                  `type` tinyint(3) unsigned NOT NULL COMMENT '文件类型',
                  `createtime` int(10) unsigned NOT NULL COMMENT '上传时间',
                  `group_id` int(20) DEFAULT NULL COMMENT '分组id',
                  `module` int(10) DEFAULT NULL COMMENT '模块名',
                  PRIMARY KEY (`id`),
                  KEY `idx_module` (`module`)
                ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='附件表';";
        Db::execute($sql);
        if (empty(DB::query("SHOW INDEX FROM yx_sys_attachment WHERE Column_name = 'module';"))) {
            Db::execute("ALTER TABLE `yx_sys_attachment` ADD INDEX `idx_module` (`module`)");
        }
    }

    public static function create_platform()
    {
        $sql = " CREATE TABLE IF NOT EXISTS `yx_sys_platform` (
                  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
                  `token` varchar(32) NOT NULL,
                  `encodingaeskey` varchar(255) NOT NULL,
                  `platform_type` tinyint(2) unsigned NOT NULL DEFAULT '1' COMMENT ' 1微信小程序 2微信公众号 ',
                  `name` varchar(30) NOT NULL,
                  `account` varchar(30) NOT NULL,
                  `original` varchar(50) NOT NULL,
                  `signature` varchar(100) DEFAULT NULL,
                  `appid` varchar(50) NOT NULL,
                  `secret` varchar(50) NOT NULL,
                  `isyixuan` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '是否易宣自有 1是 0不是',
                  `isdeposit` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否服务商托管 1是 0不是',
                  `mchid` bigint(10) DEFAULT NULL COMMENT '支付商户号',
                  `apikey` char(32) DEFAULT NULL COMMENT '支付秘钥',
                  `serial_no` varchar(64) DEFAULT NULL COMMENT '证书序列号',
                  `apiclient_cert` text COMMENT 'apiclient_cert.pem 证书',
                  `apiclient_key` text COMMENT 'apiclient_key.pem 证书',
                  `status` tinyint(1) DEFAULT NULL COMMENT '状态 1正常 0暂停',
                  `logo` varchar(255) DEFAULT NULL COMMENT 'logo地址',
                  `use_num` int(10) DEFAULT '0' COMMENT '使用次数',
                  `template_msg` json DEFAULT NULL COMMENT '模版消息',
                  `create_time` datetime DEFAULT NULL,
                  PRIMARY KEY (`id`) USING BTREE,
                  KEY `idx_key` (`appid`)
                ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
        ";
        $result = Db::execute($sql);
    }

    public static function create_modules()
    {
        $sql = " CREATE TABLE IF NOT EXISTS `yx_sys_modules` (
                 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
                  `module_name` varchar(100) NOT NULL,
                  `mpid` int(10) NOT NULL COMMENT '公众号ID',
                  `themeid` int(10) NOT NULL,
                  `system` json DEFAULT NULL,
                  `theme` json DEFAULT NULL,
                  `config` json DEFAULT NULL,
                  `softkey` varchar(10) DEFAULT NULL,
                  `new_theme_cache` text DEFAULT NULL,
                  `update_time` int(11) NULL DEFAULT 0,
                  PRIMARY KEY (`id`) USING BTREE,
                  KEY `idx_name` (`module_name`) USING BTREE
              ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8";
        $result = Db::execute($sql);
        if (!db_fieldexists('yx_sys_modules', 'update_time')) {
            Db::execute("ALTER TABLE `yx_sys_modules` ADD COLUMN `update_time` int(11) NULL DEFAULT 0 AFTER `new_theme_cache`;");
        }
    }

    public static function create_users()
    {
        $sql = "CREATE TABLE IF NOT EXISTS `yx_sys_users` (
              `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
              `groupid` int(10) unsigned NOT NULL,
              `username` varchar(30) NOT NULL,
              `password` varchar(200) NOT NULL,
              `realname` varchar(50) DEFAULT NULL COMMENT '真实姓名',
              `salt` varchar(10) NOT NULL,
              `user_level` tinyint(3) unsigned NOT NULL DEFAULT '5' COMMENT '1 超级管理员 2内部员工 3秀场公司 5活动用户',
              `status` tinyint(4) NOT NULL,
              `joindate` int(10) unsigned NOT NULL,
              `lastvisit` int(10) unsigned NOT NULL,
              `remark` varchar(500) DEFAULT NULL,
              `starttime` int(10) unsigned NOT NULL,
              `endtime` int(10) unsigned NOT NULL,
              `session_id` varchar(50) DEFAULT NULL COMMENT '登录sessiion_id',
              `single_salt` char(10) DEFAULT NULL COMMENT '单点登录key',
              `single_enc` varchar(200) DEFAULT NULL COMMENT '单点登录加密',
              `single_endtime` int(10) DEFAULT NULL COMMENT '单点登录过期时间',
              `sheji_salt` char(16) DEFAULT NULL COMMENT '设计后台登录验证',
              `module_name` varchar(10) DEFAULT NULL COMMENT '活动编号',
              `fail_count` int(2) DEFAULT 0,
              `fail_last_time` int(10) DEFAULT NULL,
              PRIMARY KEY (`id`) USING BTREE,
              UNIQUE KEY `username` (`username`)
         ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC";
        $result = Db::execute($sql);
    }


    /**
     * Desc:
     * User: yixuan
     * Time: 2016/9/17 09:12
     */
    public static function new_server()
    {

        $url = 'https://oa.kissbaby.net.cn/app/index.php?i=2&c=entry&do=getbanklist&m=yx99999&op=get_users';
        $res = yxcurl($url, [], [], true);
        $inser_data = $res['message']['data'];
        $a = Db::table('yx_sys_users')->insertAll($inser_data);
        var_dump($a, $res['message']['data']);
        die;
    }


}